package com.example.home.metro.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.metro.adapter.PathNameAdapter;
import com.example.home.metro.bean.PathImgBean;
import com.example.home.metro.bean.PathNameBean;
import com.example.home.tool.OKHttpTool;

import static com.example.home.tool.StaticActi.ip;

public class PathImgActivity extends AppCompatActivity {
    private ImageView back;
    private RecyclerView rcvList;
    private ImageView imgUrl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_path_img);

        back = findViewById(R.id.back);
        rcvList = findViewById(R.id.rcv_list);
        imgUrl = findViewById(R.id.imgUrl);
        getPathImg();
        getPathName();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PathImgActivity.this.finish();
            }
        });
    }

    private void getPathName() {
        OKHttpTool.doNetRequest("prod-api/api/metro/line/list",
                OKHttpTool.GET,
                PathNameBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        PathNameBean pathNameBean = (PathNameBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindName(pathNameBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindName(PathNameBean pathNameBean) {
        PathNameAdapter pathNameAdapter = new PathNameAdapter(PathImgActivity.this,pathNameBean.getData());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PathImgActivity.this);
        rcvList.setAdapter(pathNameAdapter);
        rcvList.setLayoutManager(linearLayoutManager);
    }

    private void getPathImg() {
        OKHttpTool.doNetRequest("prod-api/api/metro/city",
                OKHttpTool.GET,
                PathImgBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        PathImgBean pathImgBean = (PathImgBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Glide.with(PathImgActivity.this).load(ip+pathImgBean.getData().getImgUrl()).into(imgUrl);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }
}